package modbus;

/**
 * @Author : 托马斯
 * @Date : 2025/7/3 9:47
 * @Description :
 */
public class Type {

    public static void main(String[] args) {
        int A = -19;
        int B = 28836;
        System.out.println(get16Char((short)A));
        System.out.println(get16Char((short)B));
        System.out.println(getAB(get16Char((short)A)));
        System.out.println(getBA(get16Char((short)A)));
        System.out.println(getABCD(get16Char((short)A), get16Char((short)B)));
        System.out.println(getCDAB(get16Char((short)A), get16Char((short)B)));
    }

    private static String get16Char(Short s) {
        String str =  String.format("%16s", Integer.toBinaryString(s)).replace(' ', '0');
        int length = str.length() - 16;
        return str.substring(length);
    }

    private static Short getShort(String s) {
        return (short) Integer.parseInt(s, 2);
    }

    private static Float getFloat(String s) {
        int flag = s.charAt(0) == '1' ? -1 : 1;
        s = '0' + s.substring(1);
        Integer intBits = Integer.parseInt(s, 2);
        return Float.intBitsToFloat(intBits) * flag;
    }

    private static Float getABCD(String AB, String CD) {
        String str = AB + CD;
        return getFloat(str);
    }

    private static Float getCDAB(String AB, String CD) {
        String str = CD + AB;
        return getFloat(str);
    }

    private static Short getAB(String s) {
        return getShort(s);
    }

    private static Short getBA(String s) {
        String A = s.substring(0, 8);
        String B = s.substring(8);
        return getShort(B + A);
    }

}
